<template>
  <!-- 表单 -->
  <el-form ref="addForm" label-width="100px" :model="ruForm" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="ruForm.username" style="width:80%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="ruForm.mobile" style="width:80%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="ruForm.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="ruForm.formOfEmployment" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="ruForm.workNumber" style="width:80%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="ruForm.departmentName" style="width:80%" placeholder="请选择部门" @focus="hFocus" />
      <div v-if="showTree" class="tree-box">
        <el-tree :data="treelist" :props="{ label: 'name' }" @node-click="hNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="ruForm.correctionTime" style="width:80%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addEmployee } from '@/api/employees'
import employees from '@/constant/employees'
import { getDepartments } from '@/api/departments'
import { tranList } from '@/utils'
export default {
  data() {
    return {
      // 部门树状分层
      treelist: [],
      //   部门列表选项开关
      showTree: false,
      //   表单内容
      ruForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      //   多选内容
      hireType: employees.hireType,
      //   验证规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {

  },
  methods: {
    // 部门表单框聚焦时显示
    hFocus() {
      this.loadDepartments()
      this.showTree = true
    },
    // 部门表单框选中内容填充并取消列表
    hNodeClick(data, node) {
    //   判断是否有子集,有就不能选中
      if (data.children.length !== 0) {
        return
      }
      //   填充
      this.ruForm.departmentName = data.name
      //   关闭列表盒子
      this.showTree = false
    },
    // 兜底校验
    hSubmit() {
      this.$refs.addForm.validate(valid => {
        if (!valid) return // 校验失败
        this.addlist()
      })
    },
    // 取消
    hCancel() {
      this.$emit('aaa')
    },
    // 新增函数
    async addlist() {
      try {
        const res = await addEmployee(this.ruForm)
        this.$emit('success')
        console.log('新增角色', res)
        // 保存数据
      } catch (err) {
        console.log(err)
      }
    },
    // 获得部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        res.data.depts.shift()
        this.treelist = tranList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 清空
    resetForm() {
      this.ruForm = {}
      this.$refs.addForm.resetFields()
    }

  }
}
</script>

<style>
.tree-box{
    position: absolute;
    width: 50%;
    height: 150px;
    overflow-y:auto;
    z-index: 1;
}
</style>
